#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Convenience wrapper for starting an appengine tool."""


import os
import sys

if not hasattr(sys, 'version_info'):
  sys.stderr.write('Very old versions of Python are not supported. Please '
                   'use version 2.5 or greater.\n')
  sys.exit(1)
version_tuple = tuple(sys.version_info[:2])
if version_tuple < (2, 4):
  sys.stderr.write('Error: Python %d.%d is not supported. Please use '
                   'version 2.5 or greater.\n' % version_tuple)
  sys.exit(1)
if version_tuple == (2, 4):
  sys.stderr.write('Warning: Python 2.4 is not supported; this program may '
                   'break. Please use version 2.5 or greater.\n')

DIR_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
#SCRIPT_DIR = os.path.join(DIR_PATH, 'google', 'appengine', 'tools')
SCRIPT_DIR = '/usr/local/bin'

EXTRA_PATHS = [
  DIR_PATH,
  os.path.join(DIR_PATH, 'lib', 'antlr3'),
  os.path.join(DIR_PATH, 'lib', 'django'),
  os.path.join(DIR_PATH, 'lib', 'webob'),
  os.path.join(DIR_PATH, 'lib', 'yaml'),
  os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
  os.path.join(DIR_PATH, 'gozertest'),
]

SCRIPT_EXCEPTIONS = {
  "dev_appserver.py" : "dev_appserver_main.py"
}

def run_file(file_path, globals_, locals_, script_dir=SCRIPT_DIR):
  """Execute the file at the specified path with the passed-in globals."""
  #sys.path = EXTRA_PATHS + sys.path
  script_name = os.path.basename(file_path)
  script_name = SCRIPT_EXCEPTIONS.get(script_name, script_name)
  script_path = os.path.join(script_dir, script_name)
  execfile(script_path, globals_, locals_)

sys.path = EXTRA_PATHS + sys.path

#from gozerlib.config import Config

#config = Config('mainconfig')
#
#APP_ID = config['app_id']
APP_ID = 'gozerwave'
AUTH_DOMAIN = 'gmail.com'
LOGGED_IN_USER = 'bthate@gmail.com' # set to '' for no logged in user

from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google.appengine.api import urlfetch_stub
from google.appengine.api import user_service_stub
from google.appengine.api.memcache import memcache_stub
 
#
# Start with a fresh api proxy.
#
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
 
#
# Use a fresh stub datastore.
#
stub = datastore_file_stub.DatastoreFileStub(APP_ID, '/dev/null', '/dev/null')
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
os.environ['APPLICATION_ID'] = APP_ID
 
#
# Use a fresh stub UserService.
#
apiproxy_stub_map.apiproxy.RegisterStub('user',
user_service_stub.UserServiceStub())
os.environ['AUTH_DOMAIN'] = AUTH_DOMAIN
os.environ['USER_EMAIL'] = LOGGED_IN_USER
 
#
# Use a fresh urlfetch stub.
#
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())
 
#
# Use a fresh mail stub.
#
apiproxy_stub_map.apiproxy.RegisterStub(
'mail', mail_stub.MailServiceStub())
 
#
# Use a fresh memcache stub.
#
apiproxy_stub_map.apiproxy.RegisterStub('memcache', memcache_stub.MemcacheServiceStub())

if __name__ == '__main__':
  run_file('sphinx-build', globals(), locals())
